package com.example.myimageshare.View;

import android.content.Intent;
import android.os.Bundle;

import com.example.myimageshare.domain.User;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myimageshare.R;
import com.google.android.material.textfield.TextInputEditText;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class PasswordActivity extends AppCompatActivity {

    //组件
    private Toolbar toolbar;
    private TextView tv_username;
    private TextInputEditText tiet_old_pwd;
    private TextInputEditText tiet_new_pwd;
    private TextInputEditText tiet_again_new_pwd;
    private Button btn_confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        initView();
        initDate();
        initListener();

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void initView(){
        toolbar = findViewById(R.id.toolbar_pwd);
        tv_username =findViewById(R.id.tv_pwd_username);
        tiet_old_pwd = findViewById(R.id.tiet_info_old_pwd);
        tiet_new_pwd = findViewById(R.id.tiet_info_new_pwd);
        tiet_again_new_pwd = findViewById(R.id.tiet_again_new_pwd);
        btn_confirm = findViewById(R.id.bt_pwd_update);
    }

    private void initDate(){
        User user  = BmobUser.getCurrentUser(User.class);
        tv_username.setText(user.getUsername());
    }

    private void initListener(){

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(check()){
                    updatePassword();
                }else {
                    showMsg("请检查输入");
                }
            }
        });

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

    private boolean check(){


        if(tiet_old_pwd.getText().toString().trim().length()<6
                ||tiet_new_pwd.getText().toString().trim().length()<6
        || tiet_again_new_pwd.getText().toString().trim().length()<6){
            showMsg("用户密码不能小于6位");
            return false;
        }
        if(tiet_old_pwd.getText().toString().trim().length()>16
                ||tiet_new_pwd.getText().toString().trim().length()>16
        || tiet_again_new_pwd.getText().toString().trim().length()>16){
            showMsg("用户密码不能大于16位");
            return false;
        }
        if(!tiet_again_new_pwd.getText().toString().trim()
                .equals(tiet_new_pwd.getText().toString().trim())){
            showMsg("两次密码输入不一致");
            return false;
        }

        return true;
    }

    private void updatePassword(){
        BmobUser.updateCurrentUserPassword(tiet_old_pwd.getText().toString().trim(),
                tiet_again_new_pwd.getText().toString().trim(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    showMsg("验证成功，密码已修改");
                    Intent intent = new Intent(PasswordActivity.this,
                            LoginActivity.class);
                    BmobUser.logOut();
                    startActivity(intent);
                    finish();
                } else {
                    showMsg("原密码输入错误，请检查输入");
                }
            }
        });
    }

    private void showMsg(String msg){
        Toast.makeText(PasswordActivity.this,msg,Toast.LENGTH_SHORT).show();
    }


}
